package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysModuleAccessLogDAO;
import com.ourteam.system.dao.SysModuleAccessLog;
import com.ourteam.system.dao.SysModuleAccessLogDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:39
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysModuleAccessLogDAOJdbcImpl extends AbstractSpringDAO
    implements ISysModuleAccessLogDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ModuleId, UserId, SessionId, AccessTime, Status, Remarks,
            ModuleName, DomainId, DomainName, UserName, LoginTime
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ModuleId,
            _DataObjectProperty.UserId, _DataObjectProperty.SessionId,
            _DataObjectProperty.AccessTime, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks, _DataObjectProperty.ModuleName,
            _DataObjectProperty.DomainId, _DataObjectProperty.DomainName,
            _DataObjectProperty.UserName, _DataObjectProperty.LoginTime
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysModuleAccessLog, SysModuleAccessLogDAOQueryBean> daoHelper;

    public AbstractSysModuleAccessLogDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysModuleAccessLog, SysModuleAccessLogDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysModuleDAO.EntityName)
                                .append(" SYS_MODULE ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysModuleDAO.Id,
                    com.ourteam.system.dao.ISysModuleDAO.EntityName + ".",
                    "SYS_MODULE."));
            relatedJoinSQLBuffer.append(" = ").append(ModuleId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDomainDAO.EntityName)
                                .append(" SYS_DOMAIN ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDomainDAO.Id,
                    com.ourteam.system.dao.ISysDomainDAO.EntityName + ".",
                    "SYS_DOMAIN."));
            relatedJoinSQLBuffer.append(" = ").append(DomainId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" SYS_USER ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "SYS_USER."));
            relatedJoinSQLBuffer.append(" = ").append(UserId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserLoginLogDAO.EntityName)
                                .append(" SYS_USER_LOGIN_LOG ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserLoginLogDAO.SessionId,
                    com.ourteam.system.dao.ISysUserLoginLogDAO.EntityName +
                    ".", "SYS_USER_LOGIN_LOG."));
            relatedJoinSQLBuffer.append(" = ").append(SessionId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysModuleAccessLog
         * @throws Exception
         */
    public boolean checkIsUnique(SysModuleAccessLog aSysModuleAccessLog)
        throws Exception {
        if (aSysModuleAccessLog == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysModuleAccessLog.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysModuleAccessLog
     * @throws Exception
     */
    public void insert(SysModuleAccessLog aSysModuleAccessLog)
        throws Exception {
        super.doInsert(aSysModuleAccessLog);
    }

    /**
         * Batch Insert Datas
         * @param aSysModuleAccessLog
         * @throws Exception
         */
    public void batchInsert(SysModuleAccessLog[] aSysModuleAccessLogs)
        throws Exception {
        super.doBatchInsert(aSysModuleAccessLogs);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysModuleAccessLogDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysModuleAccessLog aSysModuleAccessLog)
        throws Exception {
        delete(aSysModuleAccessLog.getId());
    }

    /**
     * Update Data Selective
     * @param aSysModuleAccessLog
     * @throws Exception
     */
    public void update(SysModuleAccessLog aSysModuleAccessLog)
        throws Exception {
        if (aSysModuleAccessLog.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysModuleAccessLog);

        SysModuleAccessLog tempObj = findById(aSysModuleAccessLog.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysModuleAccessLog, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysModuleAccessLogs[]
     * @throws Exception
     */
    public void batchUpdate(SysModuleAccessLog[] aSysModuleAccessLogs)
        throws Exception {
        super.doBatchUpdate(aSysModuleAccessLogs);
    }

    /**
         * Update Data By Condictions
         * @param aSysModuleAccessLog
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysModuleAccessLog aSysModuleAccessLog,
        SysModuleAccessLogDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysModuleAccessLog, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysModuleAccessLogDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysModuleAccessLog aSysModuleAccessLog)
        throws Exception {
        if (aSysModuleAccessLog.getSessionId() == null) {
            throw new Exception(
                "The SysModuleAccessLog.sessionId value is not allow NULL");
        }

        if (aSysModuleAccessLog.getAccessTime() == null) {
            throw new Exception(
                "The SysModuleAccessLog.accessTime value is not allow NULL");
        }

        if (aSysModuleAccessLog.getStatus() == null) {
            throw new Exception(
                "The SysModuleAccessLog.status value is not allow NULL");
        }

        if (aSysModuleAccessLog.getRemarks() == null) {
            throw new Exception(
                "The SysModuleAccessLog.remarks value is not allow NULL");
        }

        if (aSysModuleAccessLog.getDomainName() == null) {
            throw new Exception(
                "The SysModuleAccessLog.domainName value is not allow NULL");
        }

        if (aSysModuleAccessLog.getModuleName() == null) {
            throw new Exception(
                "The SysModuleAccessLog.moduleName value is not allow NULL");
        }

        if (aSysModuleAccessLog.getUserName() == null) {
            throw new Exception(
                "The SysModuleAccessLog.userName value is not allow NULL");
        }

        if (aSysModuleAccessLog.getLoginTime() == null) {
            throw new Exception(
                "The SysModuleAccessLog.loginTime value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysModuleAccessLog findById(final long id) throws Exception {
        SysModuleAccessLogDAOQueryBean queryBean = new SysModuleAccessLogDAOQueryBean();

        SysModuleAccessLogDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysModuleAccessLog[] SysModuleAccessLogs = query(queryBean);

        if (SysModuleAccessLogs.length == 0) {
            return null;
        } else {
            return SysModuleAccessLogs[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysModuleAccessLog[]
         * @throws Exception
         */
    public SysModuleAccessLog[] query(SysModuleAccessLogDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcSysModuleAccessLogRowMapper());

        return (SysModuleAccessLog[]) result.toArray(new SysModuleAccessLog[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysModuleAccessLogDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysModuleAccessLogDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysModuleAccessLog[] aSysModuleAccessLogs,
        SysModuleAccessLogDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysModuleAccessLogs, queryBean);
    }

    public void synchronize(SysModuleAccessLog[] aSysModuleAccessLogs,
        SysModuleAccessLogDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysModuleAccessLog> callBack)
        throws Exception {
        daoHelper.synchronize(aSysModuleAccessLogs, queryBean, callBack);
    }

    public static class JdbcSysModuleAccessLogRowMapper implements RowMapper<SysModuleAccessLog> {
        public SysModuleAccessLog mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysModuleAccessLog object = new SysModuleAccessLog();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object moduleId = null;

                moduleId = resultSet.getObject(columnIndex++);

                object.setModuleIdNull(resultSet.getBoolean(columnIndex++));

                if (moduleId != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleId, moduleId);
                }

                Object userId = null;

                userId = resultSet.getObject(columnIndex++);

                object.setUserIdNull(resultSet.getBoolean(columnIndex++));

                if (userId != null) {
                    setBeanValue(object, _DataObjectProperty.UserId, userId);
                }

                Object sessionId = null;

                sessionId = resultSet.getObject(columnIndex++);

                object.setSessionIdNull(resultSet.getBoolean(columnIndex++));

                if (sessionId != null) {
                    setBeanValue(object, _DataObjectProperty.SessionId,
                        sessionId);
                }

                Object accessTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    accessTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    accessTime = resultSet.getObject(columnIndex++);
                }

                object.setAccessTimeNull(resultSet.getBoolean(columnIndex++));

                if (accessTime != null) {
                    setBeanValue(object, _DataObjectProperty.AccessTime,
                        accessTime);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object moduleName = null;

                moduleName = resultSet.getObject(columnIndex++);

                object.setModuleNameNull(resultSet.getBoolean(columnIndex++));

                if (moduleName != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleName,
                        moduleName);
                }

                Object domainId = null;

                domainId = resultSet.getObject(columnIndex++);

                object.setDomainIdNull(resultSet.getBoolean(columnIndex++));

                if (domainId != null) {
                    setBeanValue(object, _DataObjectProperty.DomainId, domainId);
                }

                Object domainName = null;

                domainName = resultSet.getObject(columnIndex++);

                object.setDomainNameNull(resultSet.getBoolean(columnIndex++));

                if (domainName != null) {
                    setBeanValue(object, _DataObjectProperty.DomainName,
                        domainName);
                }

                Object userName = null;

                userName = resultSet.getObject(columnIndex++);

                object.setUserNameNull(resultSet.getBoolean(columnIndex++));

                if (userName != null) {
                    setBeanValue(object, _DataObjectProperty.UserName, userName);
                }

                Object loginTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    loginTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    loginTime = resultSet.getObject(columnIndex++);
                }

                object.setLoginTimeNull(resultSet.getBoolean(columnIndex++));

                if (loginTime != null) {
                    setBeanValue(object, _DataObjectProperty.LoginTime,
                        loginTime);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
